//
//  MinorChartKLineView.mm
//  ChartRefactor
//
//  Created by CaoJun on 15/4/3.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "MinorChartKLineView.h"

using namespace ytx;

@interface MinorChartKLineView()

@property (nonatomic, assign) Quotation * parentData;

@end

@implementation MinorChartKLineView

-(void)constructor
{
    [super constructor];
    
    CGRect bounds = self.bounds;
    
    RectangleOption option = RectangleOption();
    
    option.paddingBottom = 2;
    
    Rectangle rectMain(bounds.size.width, bounds.size.height, option);
    
    self.indexPosition->setRectangle(rectMain);
    
    [self addSubview:self.rectPainterView];
    
    [self addSubview:self.indexPainterView];
    
    [self addSubview:self.linePainterView];
    
    [self addSubview:self.textPainterView];
    
    [self addSubview:self.crosshairsPainterView];
    
    self.rectPainterView.indexPosition = self.indexPosition;
    
    self.linePainterView.indexPosition = self.indexPosition;
    
    self.indexPainterView.indexPosition = self.indexPosition;
    
    self.textPainterView.indexPosition = self.indexPosition;
    
    self.crosshairsPainterView.indexPosition = self.indexPosition;
    
}

-(void)render:(ytx::IndexData *)data kLineData:(ytx::KLineData *)kLineData
{
    if (data == NULL) {
        return;
    }
    self.indexData = data;
    self.parentData = data->getParentData();
    [self resize];
    self.indexPosition->initViewIndexByDataIndexAndRect(data, data->getDrawingCount(), Config::getInstance()->kLine.min, Config::getInstance()->kLine.max);
    self.indexPosition->indexType = kLineData->indexType;
    [self paint];
}

-(void)renderKeepIndexPosition:(IndexData *) data kLineData:(ytx::KLineData *)kLineData
{
    if (data == NULL) {
        return;
    }
    
    if(!self.indexPosition->hadInited() || data->getParentData() != self.parentData  ){
        self.indexPosition->initViewIndexByDataIndexAndRect(data, data->getDrawingCount(), Config::getInstance()->kLine.min, Config::getInstance()->kLine.max);
    }
    [self resize];
    self.indexPosition->concatCount(data);
    self.indexPosition->setPara(data);
    self.indexData = data;
    self.indexPosition->indexType = kLineData->indexType;
    self.parentData = data->getParentData();
    [self paint];
}

-(void)render
{
    if (![self hasData]) {
        return;
    }
    [self resize];
    self.indexPosition->setPara(self.indexData);
    [self paint];
}

-(void)paint{
    switch (self.indexData->indexType) {
        case IndexType::VOLUME:
            [self.rectPainterView paint: self.indexPosition withPainter:[PainterRect new] ];
            [self.indexPainterView paint: self.indexPosition withPainter: [PainterIndexVolume new] ];
            [self.linePainterView paint: self.indexPosition withPainter: [PainterLineIndex new] ];
            [self.textPainterView paint: self.indexPosition withPainter:[PainterTextVolume new] ];
            break;
        case IndexType::Tuov:
            [self.rectPainterView paint: self.indexPosition withPainter:[PainterRect new] ];
            [self.indexPainterView paint: self.indexPosition withPainter: [PainterIndexTuov new] ];
            //[self.linePainterView clear];
            [self.linePainterView paint: self.indexPosition withPainter: [PainterLineIndex new] ];
            [self.textPainterView paint: self.indexPosition withPainter:[PainterTextTuov new] ];
            break;
        case IndexType::MACD:
            [self.rectPainterView paint: self.indexPosition withPainter:[PainterRect new] ];
            [self.indexPainterView paint: self.indexPosition withPainter: [PainterIndexMACD new] ];
            [self.linePainterView paint: self.indexPosition withPainter: [PainterLineIndex new] ];
            [self.textPainterView paint: self.indexPosition withPainter:[PainterText new] ];
            break;
        case IndexType::KDJ:
            [self.rectPainterView paint: self.indexPosition withPainter:[PainterRectKDJ new] ];
            [self.indexPainterView clear];
            [self.linePainterView paint: self.indexPosition withPainter: [PainterLineIndex new] ];
            [self.textPainterView paint: self.indexPosition withPainter:[PainterTextKDJ new] ];
            break;
        case IndexType::RSI:
            [self.rectPainterView paint: self.indexPosition withPainter:[PainterRect new] ];
            [self.indexPainterView clear];
            [self.linePainterView paint: self.indexPosition withPainter: [PainterLineIndex new] ];
            [self.textPainterView paint: self.indexPosition withPainter:[PainterTextIndexPercent new] ];
            break;
        case IndexType::BIAS:
            [self.rectPainterView paint: self.indexPosition withPainter:[PainterRect new] ];
            [self.indexPainterView clear];
            [self.linePainterView paint: self.indexPosition withPainter: [PainterLineIndex new] ];
            [self.textPainterView paint: self.indexPosition withPainter:[PainterText new] ];
            break;
        //ADDIndex:
        case IndexType::CCI:
            [self.rectPainterView paint: self.indexPosition withPainter:[PainterRect new] ];
            [self.indexPainterView clear];
            [self.linePainterView paint: self.indexPosition withPainter: [PainterLineIndex new] ];
            [self.textPainterView paint: self.indexPosition withPainter:[PainterText new] ];
            break;
        default:
            break;
    }
    
    [self.crosshairsPainterView clear];
    
    [super paint];
    
}

- (BOOL) hasData
{
    return self.indexData != NULL && self.indexData->hasData();
}

- (void) clear
{
    [self.rectPainterView clear];
    [self.linePainterView clear];
    [self.textPainterView clear];
    [self.indexPainterView clear];
    [self.crosshairsPainterView clear];
    
    [super clear];
}

@end
